﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MarginTrading.Entity;
using MarginTrading.Common;
using MarginTrading.Services;

namespace MarginTrading.Forms
{
    public partial class AddTrade : Form
    {
        public AddTrade()
        {
            InitializeComponent();
        }

        private void buttonOK_Click(object sender, EventArgs e)
        {
            if (validateInputs() == false)
            {
                return;
            }

            DailyTransactions transaction = new DailyTransactions();

            transaction.TranId = "Manual";
            transaction.TradeDate = dateTimePicker.Value;
            if (comboBoxSide.Text == "BUY")
            {
                transaction.BuySell = "B";
            }
            else
            {
                transaction.BuySell = "S";
            }

            transaction.ExecutionId = GetExecutionID();
            transaction.CompanySymbol = textBoxSymbol.Text;
            transaction.Quantity = int.Parse(textBoxQuantity.Text);
            transaction.Price = double.Parse(textBoxPrice.Text);
            transaction.Amount = double.Parse(textBoxAmount.Text);
            transaction.ClientCode = textBoxClientCode.Text;
            try
            {
                Config config2 = ConfigServices.GetConfigByName(Constants.CONFIG_NAME_COMPANY_CODE);
                transaction.ParticipantId = config2.Value;
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to find broker code\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            
            transaction.Status = 1;

            try
            {
                DailyTransactionsServices.Add(transaction);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Failed to insert new trade\nError: " + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            MessageBox.Show("Trade added successfully", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);
            Close();
        }

        private bool validateInputs()
        {
            if (textBoxClientCode.Text == "" 
                || textBoxSymbol.Text == "" 
                || textBoxQuantity.Text == ""
                || textBoxPrice.Text == ""
                || textBoxAmount.Text == "")
            {
                MessageBox.Show("Please enter required details", "Required Fields not Filled", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
                return false;
            }

            int iQuantity = 0;
            if (int.TryParse(textBoxQuantity.Text, out iQuantity) == false)
            {
                MessageBox.Show("Invalid quantity", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            double fPrice = 0;
            if (double.TryParse(textBoxPrice.Text, out fPrice) == false)
            {
                MessageBox.Show("Invalid price", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            double fAmount = 0;
            if (double.TryParse(textBoxAmount.Text, out fAmount) == false)
            {
                MessageBox.Show("Invalid amount", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            Security security = null;
            try
            {
                security = SecurityServices.GetSecurityBySymbol(textBoxSymbol.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid security code   " + textBoxSymbol.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (security == null)
            {
                MessageBox.Show("Invalid security code   " + textBoxSymbol.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            Client client = null;
            try
            {
                client = ClientService.GetClientByCode(textBoxClientCode.Text);
            }
            catch (System.Exception ex)
            {
                MessageBox.Show("Invalid client code   " + textBoxClientCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            if (client == null)
            {
                MessageBox.Show("Invalid client code   " + textBoxClientCode.Text, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return false;
            }

            return true;
        }

        private void AddTrade_Load(object sender, EventArgs e)
        {
            try
            {
                AutoCompleteStringCollection data = new AutoCompleteStringCollection();

                ClientCollection clientCollection = ClientService.GetClientCollection();

                for (int iI = 0; iI < clientCollection.Count; iI++)
                {
                    data.Add(clientCollection[iI].Code);
                }

                textBoxClientCode.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                textBoxClientCode.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBoxClientCode.AutoCompleteCustomSource = data;

                data = new AutoCompleteStringCollection();

                SecurityCollection securityCollection = SecurityServices.GetSecurityCollection();

                for (int iI = 0; iI < securityCollection.Count; iI++)
                {
                    data.Add(securityCollection[iI].Symbol);
                }

                textBoxSymbol.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
                textBoxSymbol.AutoCompleteSource = AutoCompleteSource.CustomSource;
                textBoxSymbol.AutoCompleteCustomSource = data;

                comboBoxSide.SelectedIndex = 0;
            }
            catch (System.Exception ex)
            {
            }
        }

        private void textBoxClientCode_Enter(object sender, EventArgs e)
        {
            AcceptButton = null;
        }

        private void textBoxClientCode_Leave(object sender, EventArgs e)
        {
            AcceptButton = buttonOK;
        }

        private void textBoxSymbol_Enter(object sender, EventArgs e)
        {
            AcceptButton = null;
        }

        private void textBoxSymbol_Leave(object sender, EventArgs e)
        {
            AcceptButton = buttonOK;
        }

        private string GetExecutionID()
        {
            string sTimestamp = DateTime.Now.Ticks.ToString();
            sTimestamp = sTimestamp.Substring(2, 11);
            return "man" + sTimestamp;
        }
    }
}
